package com.avengers.core.kit;


import com.avengers.core.bean.enums.SymbolEnum;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author yu.wang
 * @since 2025/3/12 16:11
 **/
public class SleepKit {
    private static final Logger log = LoggerFactory.getLogger(SleepKit.class);
    private static final long DEFAULT_MILLIS = 200;

    public static void sleep() {
        sleep(DEFAULT_MILLIS);
    }

    public static void sleep(Logger logger) {
        sleep(DEFAULT_MILLIS, logger);
    }

    public static void sleep(String keyword) {
        sleep(DEFAULT_MILLIS, keyword);
    }

    public static void sleep(String keyword, Logger logger) {
        sleep(DEFAULT_MILLIS, logger);
    }

    public static void sleep(long minMillis) {
        sleep(minMillis, minMillis * 3);
    }

    public static void sleep(long minMillis, String keyword) {
        sleep(minMillis, minMillis * 3, keyword);
    }

    public static void sleep(long minMillis, Logger logger) {
        sleep(minMillis, minMillis * 3, logger);
    }

    public static void sleep(long minMillis, String keyword, Logger logger) {
        sleep(minMillis, minMillis * 3, keyword, logger);
    }

    public static void sleep(long minMillis, long maxMillis) {
        sleep(minMillis, maxMillis, log);
    }

    public static void sleep(long minMillis, long maxMillis, Logger logger) {
        sleep(minMillis, maxMillis, null, logger);
    }

    public static void sleep(long minMillis, long maxMillis, String keyword) {
        sleep(minMillis, maxMillis, keyword, null);
    }

    public static void sleep(long minMillis, long maxMillis, String keyword, Logger logger) {
        long sleepMillis = RandomUtils.nextLong(minMillis, maxMillis);
        try {
            if (StringKit.notBlank(keyword)) {
                (null == logger ? log : logger).info("{} sleep {}ms", keyword, sleepMillis);
            }
            Thread.sleep(sleepMillis);
        } catch (Exception e) {
            (null == logger ? log : logger).error("{}sleep_{}ms error: {}",
                    StringKit.isBlank(keyword) ? SymbolEnum.empty : keyword + SymbolEnum.space, sleepMillis, e.getMessage(), e);
        }
    }
}
